﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.Entity;

namespace FG_Admin_DAL
{
    public class AdminsDAL
    {
        FGSkiingEntities model = new FGSkiingEntities();
        /// <summary>
        /// 分页显示所有管理员信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public List<FG_Admin_DAL.Models.AdminsMessage> GetAdmins(int PageIndex, int PageSize)
        {
            return GetAdmins().OrderBy(p=>p.Admins_ID).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        /// <summary>
        /// 显示当前管理员信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="AdminsID"></param>
        /// <returns></returns>
        public FG_Admin_DAL.Models.AdminsMessage GetAdmins(int PageIndex, int PageSize, int AdminsID)
        {
            return GetAdmins().Where(p => p.Admins_ID == AdminsID).OrderBy(p=>p.Admins_ID).Skip((PageIndex - 1) * PageSize).Take(PageSize).FirstOrDefault();
        }
        /// <summary>
        /// 显示所有管理员信息
        /// </summary>
        /// <returns></returns>

        public List<FG_Admin_DAL.Models.AdminsMessage> GetAdmins()
        {
            #region
            List<Admins> _a = model.Admins.ToList();
            List<Role_Power> _r_p = model.Role_Power.ToList();
            List<Admins_Power> _p = model.Admins_Power.ToList();
            List<Admins_Role> _r = model.Admins_Role.ToList();
            var li = from a in _a
                       join r in _r on a.RoleID equals r.RoleID
                       join r_p in _r_p on r.RoleID equals r_p.RoleID
                       join p in _p on r_p.Admins_Power_ID equals p.Admins_Power_ID
                       select new
                       {
                           Admins_ID = a.Admins_ID,
                           Admins_Name = a.Admins_Name,
                           Admins_Account = a.Admins_Account,
                           Admins_Pwd = a.Admins_Pwd,
                           Admins_Sex = a.Admins_Sex,
                           Admins_Img = a.Admins_Img,
                           Admins_Tel = a.Admins_Tel,
                           Admins_Addr = a.Admins_Addr,
                           Admins_Age = a.Admins_Age,
                           RoleID = a.RoleID,
                           SnowPack_ID = a.SnowPack_ID,
                           Admins_Start = a.Admins_Start,
                           Admins_Power_ID = p.Admins_Power_ID,
                           Admins_Power_Name = p.Admins_Power_Name,
                           Admins_Power_Url = p.Admins_Power_Url,
                           RoleName = r.RoleName,
                           Role_Power_ID = r_p.Role_Power_ID
                       };
            List<FG_Admin_DAL.Models.AdminsMessage> list = new List<Models.AdminsMessage>();
            foreach (var a in li)
            {
                FG_Admin_DAL.Models.AdminsMessage mes = new Models.AdminsMessage();
                mes.Admins_ID = a.Admins_ID;
                mes.Admins_Name = a.Admins_Name;
                mes.Admins_Account = a.Admins_Account;
                mes.Admins_Pwd = a.Admins_Pwd;
                mes.Admins_Sex = a.Admins_Sex;
                mes.Admins_Img = a.Admins_Img;
                mes.Admins_Tel = a.Admins_Tel;
                mes.Admins_Addr = a.Admins_Addr;
                mes.Admins_Age = a.Admins_Age;
                mes.RoleID = a.RoleID;
                mes.SnowPack_ID = a.SnowPack_ID;
                mes.Admins_Start = a.Admins_Start;
                mes.Admins_Power_ID = a.Admins_Power_ID;
                mes.Admins_Power_Name = a.Admins_Power_Name;
                mes.Admins_Power_Url = a.Admins_Power_Url;
                mes.RoleName = a.RoleName;
                mes.Role_Power_ID = a.Role_Power_ID;
                list.Add(mes);
            }
            #endregion
            return list;
        }
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public int EditAdmins(FG_Admin_DAL.Models.AdminsMessage mes)
        {
            Admins ad = new Admins();
            ad.Admins_Account = mes.Admins_Account;
            ad.Admins_Addr = mes.Admins_Addr;
            ad.Admins_Age = mes.Admins_Age;
            ad.Admins_ID = mes.Admins_ID;
            ad.Admins_Img = mes.Admins_Img;
            ad.Admins_Name = mes.Admins_Name;
            ad.Admins_Pwd = mes.Admins_Pwd;
            ad.Admins_Sex = mes.Admins_Sex;
            ad.Admins_Start = mes.Admins_Start;
            ad.Admins_Tel = mes.Admins_Tel;
            model.Entry(ad).State = System.Data.EntityState.Modified;
            return model.SaveChanges();

        }
        /// <summary>
        /// 修改管理员角色权限
        /// </summary>
        /// <param name="RID">角色编号</param>
        /// <param name="str">权限编号字符串</param>
        /// <returns></returns>
        public int EditPower(int RID,string str)
        {
            model.Role_Power.Where(p => p.RoleID == RID).Reverse();
            foreach (var i in str.Split(','))
            {
                Role_Power r_p = new Role_Power();
                r_p.RoleID = RID;
                r_p.Role_Power_ID = Convert.ToInt32(i);
                model.Role_Power.Add(r_p);
            }
            return model.SaveChanges();
        }
        /// <summary>
        /// 显示相关角色的权限信息
        /// </summary>
        /// <param name="RID"></param>
        /// <returns></returns>
        public List<Admins_Power> GetPower(int RID)
        {
            var list = from a in model.Admins_Power.ToList()
                       join b in model.Role_Power.ToList() on a.Admins_Power_ID equals b.Admins_Power_ID
                       join c in model.Admins_Role on b.RoleID equals c.RoleID
                       where !(a.Admins_Power_Url.Contains("/FG_Background_UI/Admin/")) && c.RoleID==RID
                       select a;
            List<Admins_Power> li = new List<Admins_Power>();
            foreach (var i in list)
            {
                Admins_Power A_P = new Admins_Power();
                A_P.Admins_Power_ID = i.Admins_Power_ID;
                A_P.Admins_Power_Name = i.Admins_Power_Name;
                A_P.Admins_Power_Url = i.Admins_Power_Url;
                li.Add(A_P);
            }
            return li;
        }
        /// <summary>
        /// 登陆人员验证
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Pwd"></param>
        /// <returns></returns>
        public int LoginDo(string Name, string Pwd)
        {
            var i = from a in model.Admins.Where(p => p.Admins_Account == Name && p.Admins_Pwd == Pwd).ToList()
                    select new
                    {
                        ss = a.RoleID,
                    };
            try
            {
                return Convert.ToInt32(i.FirstOrDefault().ss);
            }
            catch (Exception e)
            {
                return 0;
            }
        }
    }
}
